package com.lottery.ruok.lottrey.ui;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;

/**
 * Created by ASUS on 2017/9/13.
 */

public class MyGridView extends GridView {
    private boolean canListScroll = false;

    public void setCanListScroll(boolean canListScroll) {
        this.canListScroll = canListScroll;
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private float lastY;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(canListScroll) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                getParent().getParent().requestDisallowInterceptTouchEvent(true);
            } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
                if (lastY > ev.getY()) {
                    // 如果是向上滑动，且不能滑动了，则让ScrollView处理
                    if (!canScrollList(1)) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        return false;
                    }
                } else if (ev.getY() > lastY) {
                    // 如果是向下滑动，且不能滑动了，则让ScrollView处理
                    if (!canScrollList(-1)) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        return false;
                    }
                }
            }
            lastY = ev.getY();
            return super.dispatchTouchEvent(ev);
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

}
